<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('template.fields.awardsName')" prop="awardsName">
        <el-input
          v-model="queryParams.awardsName"
          :placeholder="t('common.inputTextPlaceholder') + t('template.fields.awardsName')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" :disabled="advancedSearching">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery" :disabled="advancedSearching">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          @click="openForm('create')"-->
        <!--          v-hasPermi="['awards:template:create']"-->
        <!--        >-->
        <!--          <Icon icon="ep:plus" class="mr-5px" />-->
        <!--          {{ t('action.add') }}-->
        <!--        </el-button>-->
        <!--        <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          @click="handleImport"-->
        <!--          v-hasPermi="['awards:template:export']"-->
        <!--        >-->
        <!--          <Icon icon="ep:upload" class="mr-5px" />-->
        <!--          {{ t('action.import') }}-->
        <!--        </el-button>-->
        <!--        <el-button-->
        <!--          type="success"-->
        <!--          plain-->
        <!--          @click="handleExport"-->
        <!--          :loading="exportLoading"-->
        <!--          v-hasPermi="['awards:template:export']"-->
        <!--        >-->
        <!--          <Icon icon="ep:download" class="mr-5px" />-->
        <!--          {{ t('action.export') }}-->
        <!--        </el-button>-->
        <!--        <ConditionTree-->
        <!--          id="template-condition-tree"-->
        <!--          :schemata="templateSchema"-->
        <!--          @search="handleAdvanceSearch"-->
        <!--        />-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iTemplate-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('template.fields.awardsName')"
        field="awardsName"
        align="left"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['awards:template:update']"
          >
            {{ t('action.edit') }}
          </el-button>
          <!--          <el-button-->
          <!--            link-->
          <!--            type="danger"-->
          <!--            @click="handleDelete(scope.row.id)"-->
          <!--            v-hasPermi="['awards:template:delete']"-->
          <!--          >-->
          <!--            {{ t('action.delete') }}-->
          <!--          </el-button>-->
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <TemplateForm ref="formRef" @success="getList" />
  <ExcelImportForm
    ref="importFormRef"
    object-name="奖项"
    upload-url="/awards/template/import"
    template-url="/awards/template/get-import-template"
    @success="getList"
  />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { TemplateApi, TemplateVO } from '@/api/awards/template'
import TemplateForm from './TemplateForm.vue'
import ExcelImportForm from '@/components/ExcelImportForm/index.vue'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'

/** 奖项 列表 */
defineOptions({ name: 'Template' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<TemplateVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  advanced: undefined,
  awardsName: undefined,
  onlineApply: undefined,
  conditionJson: undefined,
  qualificationReview: undefined,
  differenceSelection: undefined,
  preliminaryEvaluation: undefined,
  awardsType: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TemplateApi.getTemplatePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof TemplateForm>>()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 导入 */
const importFormRef = ref<InstanceType<typeof ExcelImportForm>>()
const handleImport = () => {
  importFormRef.value?.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TemplateApi.deleteTemplate(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await TemplateApi.exportTemplate(queryParams)
    download.excel(data, '奖项.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleAdvanceSearch = (base64Advanced?: string) => {
  ;(queryParams as any).advanced = base64Advanced
  advancedSearching.value = !!base64Advanced
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
